package cn.itcast.service.impl;

import cn.itcast.dao.CourierDao;
import cn.itcast.domain.Courier;
import cn.itcast.service.CourierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.List;
import java.util.Optional;

/**
 * @Author： ~Mr·Weng~
 * @CreateTime： 2019年-01月-21日  15:36
 * Look at the wine in the glass toast the rich first.
 */
@Service
public class CourierServiceImpl implements CourierService {

    @Autowired
    private CourierDao courierdao;


    @Override
    @Transactional
    public void save(Courier courier) {
        courierdao.save(courier);
    }

    @Override
    public Page<Courier> pageQuery( Integer page, Integer rows) {
        PageRequest request = PageRequest.of(page-1, rows);
        return courierdao.findAll(request);
    }

    @Override
    @Transactional
    public void deleById(String[] ids,Character i) {
        for (String idStr : ids) {
            int id = Integer.parseInt(idStr);
            Optional<Courier> courier = courierdao.findById(id);
            Courier cer = courier.get();
            cer.setDeltag(i);
            courierdao.save(cer);
        }
    }
    @Override
    public Page<Courier> pageQuery(Courier courier,Integer page , Integer rows){

        Pageable pageable = PageRequest.of(page - 1, rows);

        //PageRequest request = new PageRequest(page - 1, row);
        Specification<Courier> specification = new Specification<Courier>() {
            /**
             *  自定义查询条件
             *      1.实现specification接口,(提供泛型,查询对象类型)
             *      2.实现toPredicate方法(构造条件查询)
             *      3.需要借助方法参数中两个参数root,criteriaBuilder
             * @param root :获取需要查询的对象属性（查询的任何属性都可以从根对象(实现类)中获取）
             * @param criteriaQuery :顶层查询对象，自定义查询方式（了解：一般不用）
             * @param cb   :查询的构造器，封装了很多的查询条件(模糊匹配,精准匹配等等 )
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                Path<String> courierNums = root.get("courierNum");//工号
                Path<String> standards = root.get("standard").get("name");//收派标准(standard.name)
                Path<String> companys = root.get("company");//单位
                Path<String> types = root.get("type");//取件员类型
                Predicate pcn = null;
                Predicate psn = null;
                Predicate pcp = null;
                Predicate ptp = null;

                //构造查询
                if (courier.getCourierNum()!=null){
                    pcn = cb.like(courierNums.as(String.class), "%"+courier.getCourierNum().replace(" ","")+"%");
                }
                if (courier.getStandard()!=null){
                    psn = cb.like(standards.as(String.class), "%"+courier.getStandard().getName().replace(" ","")+"%");
                }

                if (courier.getCompany()!=null){
                    pcp = cb.like(companys.as(String.class), "%"+courier.getCompany().replace(" ","")+"%");
                }

                if (courier.getType()!=null){
                    ptp = cb.like(types.as(String.class), "%"+courier.getType().replace(" ","")+"%");
                }
                //将多个条件组合到一起
                Predicate predicate = cb.and(pcn, psn, pcp, ptp);//形成多条件查询
                return predicate;
            }
        };
        /*List<Courier> courierList = courierdao.findAll(specification);
        System.out.println("courierList = " + courierList);*/
        Page<Courier> couriers = courierdao.findAll(specification, pageable);
        System.out.println("couriers = " + couriers);
        return couriers;
    }


}
